# Python3_DataProcMultiProcessing.py
# Create By GF 2023-11-27 17:24

# ----------------------------------------------------------------------------------------------------

# 数据处理 - 多进程 (Data Processing - Multi Processing).

# ----------------------------------------------------------------------------------------------------

import multiprocessing
import pandas as pd

def Pandas_Compute_Field_Segmented_Index(DataFrame, Number_Of_Segments:int):

    Max_Index:int = DataFrame.index.max()
    # ----------------------------------------------
    Number_Of_Rows_Per_Segment:int = int(Max_Index / Number_Of_Segments)
    # ----------------------------------------------
    Index_List:list = []
    # ----------------------------------------------
    Counter:int = 1
    Bgn_Idx:int = 0
    while Counter <= Number_Of_Segments:
        if (Counter == 1)                                  : Index_List.append((Bgn_Idx, Bgn_Idx + Number_Of_Rows_Per_Segment - 1))
        if (1 < Counter) and (Counter < Number_Of_Segments): Index_List.append((Bgn_Idx, Bgn_Idx + Number_Of_Rows_Per_Segment - 1))
        if (Counter == Number_Of_Segments)                 : Index_List.append((Bgn_Idx, Max_Index))
        # ------------------------------------------
        Bgn_Idx = Bgn_Idx + Number_Of_Rows_Per_Segment
        Counter = Counter + 1
    # ----------------------------------------------
    return Index_List

def Pandas_Compute_Field_Sales_Package(DataFrame, BgnIdx:int, EndIdx:int):

    DataFrame["套餐标记"] = None
    # ----------------------------------------------
    for Idx in range(BgnIdx, (EndIdx + 1)):
        Order_Number = DataFrame.loc[Idx, "订单号"]
        # ------------------------------------------
        Filtered_DataFrame   = DataFrame[DataFrame["订单号"] == Order_Number]
        Filtered_Rows_Number = Filtered_DataFrame["订单号"].count()
        Filtered_Index_List  = Filtered_DataFrame.index
        # ------------------------------------------
        if (Filtered_Rows_Number >= 2) and (DataFrame.loc[Idx, "套餐标记"] == None):
            DataFrame.loc[Filtered_Index_List, "套餐标记"] = "Package(%d)" % Order_Number
        else:
            pass
    # ----------------------------------------------
    # Function End.

def Pandas_Compute_Field_Calling_Multi_Processing(Args_1):

    SegIdx = Pandas_Compute_Field_Segmented_Index(Args_1, 5)
    # ----------------------------------------------
    Process_Pool = [multiprocessing.Process(target=Pandas_Compute_Field_Sales_Package, args=(Args_1, SegIdx[0][0], SegIdx[0][1]), name='Proc_1'),
                    multiprocessing.Process(target=Pandas_Compute_Field_Sales_Package, args=(Args_1, SegIdx[1][0], SegIdx[1][1]), name='Proc_2'),
                    multiprocessing.Process(target=Pandas_Compute_Field_Sales_Package, args=(Args_1, SegIdx[2][0], SegIdx[2][1]), name='Proc_3'),
                    multiprocessing.Process(target=Pandas_Compute_Field_Sales_Package, args=(Args_1, SegIdx[3][0], SegIdx[3][1]), name='Proc_4'),
                    multiprocessing.Process(target=Pandas_Compute_Field_Sales_Package, args=(Args_1, SegIdx[4][0], SegIdx[4][1]), name='Proc_5')]
    # ----------------------------------------------
    for Proc in Process_Pool:
        Proc.start() # -> 启动进程。
    # ----------------------------------------------
    for Proc in Process_Pool:
        Proc.join() # -> 循环等待每个进程结束。

if __name__ == "__main__":
    
    df1 = pd.read_excel("./讯捷集团_销售数据_2023年08月-10月.xlsx", sheet_name="Sheet1")
    
    # 列重命名
    Rename = df1
    # --------------------------------------------------
    Rename = Rename.rename(columns={"id":"订单号"})

    # 计算字段
    Compute = Rename
    # --------------------------------------------------
    Pandas_Compute_Field_Calling_Multi_Processing(Compute)
    
    print(Compute)